package com.swordget.dao.impl;


import com.swordget.dao.TableDefinitionV2Dao;
import com.swordget.entity.ColumnDesign;
import com.swordget.entity.TableDesign;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class TableDefinitionV2DaoImpl  implements TableDefinitionV2Dao {

    @Override
    public ColumnDesign getSerialCol() {
        ColumnDesign serial = new ColumnDesign("SERIALNO", "VARCHAR", 30, 0,
                "Y", "N", "", "", "序列号", "", "", "Y", "N", "");
        return  serial;
    }

    @Override
    public List<ColumnDesign> getCommonCols() {
        ColumnDesign createDate = new ColumnDesign("CREATEDATE", "VARCHAR", 10, 0,
                "Y", "N", "", "", "创建时间", "", "", "Y", "N", "");
        ColumnDesign createTime = new ColumnDesign("CREATETIME", "VARCHAR", 10, 0,
                "Y", "N", "", "", "创建日期", "", "", "Y", "N", "");
        ColumnDesign createUser = new ColumnDesign("CREATEUSER", "VARCHAR", 20, 0,
                "Y", "N", "", "", "创建用户", "", "", "Y", "N", "");
        ColumnDesign importId = new ColumnDesign("IMPORTID", "VARCHAR", 20, 0,
                "N", "N", "", "", "导入标识", "", "", "Y", "N", "");
        ColumnDesign status = new ColumnDesign("STATUS", "VARCHAR", 10, 0,
                "N", "N", "", "", "状态", "", "", "Y", "N", "");
        ColumnDesign lastStatus = new ColumnDesign("LASTSTATUS", "VARCHAR", 10, 0,
                "N", "N", "", "", "申请前状态", "", "", "Y", "N", "");
    /*    ColumnDesign handleName = new ColumnDesign("HANDLENAME", "VARCHAR", 50, 0,
                "N", "N", "3", "", "c", "Y", "[{id:\"\",value:\"全部\",selected:true},{id:\"申请删除\",value:\"申请删除\"},{id:\"申请修改\",value:\"申请修改\"},{id:\"审核不通过\",value:\"审核不通过\"}]", "N", "Y", "");*/
        ColumnDesign handleDate = new ColumnDesign("HANDLEDATE", "VARCHAR", 10, 0,
                "N", "N", "", "", "操作日期", "N", "", "N", "Y", "");
        ColumnDesign handleTime = new ColumnDesign("HANDLETIME", "VARCHAR", 10, 0,
                "N", "N", "", "", "操作时间", "N", "", "N", "Y", "");
        ColumnDesign handleUser = new ColumnDesign("HANDLEUSER", "VARCHAR", 30, 0,
                "N", "N", "", "", "操作人", "N", "", "N", "Y", "");
        /*ColumnDesign failReason = new ColumnDesign("FAILREASON", "VARCHAR", 255, 0,
                "N", "N", "", "", "审核不通过原因", "N", "", "N", "Y", "");*/
        ColumnDesign dataSource = new ColumnDesign("DATASOURCE", "VARCHAR", 30, 0,
                "N", "N", "", "", "数据来源", "N", "", "N", "Y", "");

        List<ColumnDesign> list = Arrays.asList(new ColumnDesign[]{createDate, createTime, createUser, importId, status,
                handleDate, handleTime, handleUser, dataSource});
        return list;
    }

    @Override
    public List<String> getColInsertSql(TableDesign tableDesign, List<ColumnDesign> columnDesigns) {
        List<String> commands = new ArrayList<>();
        String tableId = tableDesign.getTableId() == null ? System.currentTimeMillis() + Math.round(Math.random() * 1000000) + "" : tableDesign.getTableId();
        //生成表详情插入SQL
        for (int i = 0; i < columnDesigns.size(); i++) {
            ColumnDesign column = columnDesigns.get(i);
            String id = System.currentTimeMillis() + Math.round(Math.random() * 1000000)
                    + String.format("%06d", i);
            commands.add(String.format("INSERT INTO TABLEDEFINITION(SERIALNO, COLUMNNAME, COLUMNTYPE, " +
                            "COLUMNWIDTH, COLUMNPOINT, NOTNULL,NOTREPEAT, ISHIDDEN, TABLEID, TABLEORDER, COLUMNCOMMENT, " +
                            "SEARCH, COMBOURL, TEXTTYPE, DATEFORMAT, ISBUSINESS) " +
                            "VALUES('%s', '%s', '%s', %d, %d, '%s','%s', '%s', '%s', %d, '%s', '%s', '%s', '%s', '%s', '%s')",
                    id, column.getName(), column.getType(), column.getWidth(), column.getPoint(),
                    column.getNotNull(), column.getNotRepeat(), "Y".equals(column.getIsHidden()) ? column.getIsHidden() : "N", tableId, i, column.getComment(),
                    column.getSearch(), column.getComboUrl(), column.getContextType(), column.getDateFormat(),
                    "Y".equals(column.getIsBusiness()) ? column.getIsBusiness() : "N"));

        }
        tableDesign.setTableId(tableId);
        return commands;
    }
}